package com.taoist_awei.blog.security.handler;

import com.alibaba.fastjson.JSON;
import com.taoist_awei.blog.core.HttpResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class DefaultNoAuthorizationHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        HttpResult result = new HttpResult.HttpResultBuilder()
                .statusCode(401)
                .message("请先登录！")
                .notSuccess()
                .build();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.setStatus(401);
        response.getWriter().write(JSON.toJSONString(result));
    }
}
